package com.demo.web;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.SalePerson;
import com.demo.entity.User;
import com.demo.service.ISalePersonService;
import com.demo.vo.SalePersonVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/saleperson")
public class SalePersonController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISalePersonService depService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SalePerson> get(SalePerson arg) {
        SalePerson service_res = depService.selectByPrimaryKey(arg.getId());
        BaseResult<SalePerson> res = new BaseResult<SalePerson>(true, service_res);
        return res;
    }


    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(SalePerson arg, HttpSession session) {
//        SalePerson sale = (SalePerson) session.getAttribute("current");
//        BaseResult<Integer> res;
//        if (sale != null) {
//            arg.setValid(null);
//            arg.setId(sale.getId());
//            int service_res = depService.updateByPrimaryKeySelective(arg);
//            res = new BaseResult<Integer>(true, service_res);
//            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
//            session.setAttribute("current", sp);
//
//        } else {
//            res = new BaseResult<Integer>(false, "请登录!", 401);
//        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/valid", method = RequestMethod.POST)
    public BaseResult<Object> valid(SalePerson arg, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        BaseResult<Object> res;
        if (user != null) {
            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
            if (sp != null && "0".equals(sp.getValid()) && "1".equals(arg.getValid())) {
                int service_res = depService.updateByPrimaryKeySelective(arg);
                res = new BaseResult(true, service_res);
            } else {
                res = new BaseResult(false, "非法操作!", 403);
            }
        } else {
            res = new BaseResult(false, "请登录!", 401);
        }
        return res;
    }


    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SalePerson> query(SalePersonVo arg) {
        List<SalePerson> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /**
     *销售交易额统计
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statistic_query", method = RequestMethod.GET)
    public ListResult<SalePersonVo> statisticQuery(SalePersonVo arg) {
        List<SalePersonVo> list = depService.querySaleCashByPage(arg);
        int total = depService.querySaleCashCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePersonVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }



    @ResponseBody
    @RequestMapping(value = "/querySaleManager", method = RequestMethod.GET)
    public ListResult<SalePerson> querySaleManager(SalePersonVo arg) {
        List<SalePerson> list = depService.querySaleManager(arg);
        int total = depService.querySaleManagerCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/mySales", method = RequestMethod.GET)
    public ListResult<SalePerson> queryMySales(SalePersonVo arg) {
        List<SalePerson> list = depService.queryByUserIdAndPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
